﻿/*
 * Created by SharpDevelop.
 * User: Dhanesh
 * Date: 07-10-2008
 * Time: 00:28:53
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;

namespace ChallanSystem
{
	/// <summary>
	/// Description of Products.
	/// </summary>
	public partial class Products : Form
	{
		Product prod = null;
		DataSet ds = null;
		DataRow dr = null;
		DataTable dtFiltered = null;
		
		public Products()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void BtnNewClick(object sender, EventArgs e)
		{
			ClearControls();
			
			enableAllControls();
			
			btnSave.Enabled = true;
			btnCancel.Enabled = true;
			btnNew.Enabled = false;
			btnUpdate.Enabled = false;
			btnDelete.Enabled = false;
		}
		
		void BtnUpdatebtnUpdateClick(object sender, EventArgs e)
		{
			if(validate())
			{
				if(isNumericCheck())
				{
					try
					{
						if(dr != null)
						{
							int id = Convert.ToInt32(dr["Product_ID"].ToString());
							prod = new Product();
							prod.Name = txtProductName.Text;
							prod.Price_Retail = txtRetailPrice.Text != ""?txtRetailPrice.Text:"NA";
							prod.Price_SemiRetail = txtSemiRetailPrice.Text != ""?txtSemiRetailPrice.Text:"NA";
							prod.Price_Wholesale = txtWholesalePrice.Text != ""?txtWholesalePrice.Text:"NA";
							prod.Price_SemiWholesale = txtSemiWholesalePrice.Text != ""?txtSemiWholesalePrice.Text:"NA";
							
							bool ret = prod.Update(id);
							if(ret == true)
							{
								MessageBox.Show("Data updated successfully!","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
								btnSave.Enabled = false;
								btnCancel.Enabled = false;
								btnNew.Enabled = true;
								ClearControls();
								RefreshDataGrid();
							}
						}
						else
						{
							MessageBox.Show("No row selected to update.");
						}
					}
					catch(Exception expUpdate)
					{
						MessageBox.Show("Sorry... Cannot Process Your Request "+expUpdate.ToString());
					}
				}
				else
					MessageBox.Show("Please Enter Valid Data !!!");
			}
			else
				MessageBox.Show("All Fields Are Mandatory !!!");
			
		}
		
		void BtnDeleteClick(object sender, EventArgs e)
		{
			if(dr != null)
			{
				int id = Convert.ToInt32(dr["Product_ID"].ToString());
				prod = new Product();
				
				bool ret = prod.Delete(id);
				if(ret == true)
				{
					MessageBox.Show("Data deleted successfully!","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
					btnSave.Enabled = false;
					btnCancel.Enabled = false;
					btnNew.Enabled = true;
					ClearControls();
					RefreshDataGrid();
				}
			}
			else
			{
				MessageBox.Show("No row selected to delete.");
			}
		}
		
		void BtnSaveClick(object sender, EventArgs e)
		{
			if(validate())
			{
				if(isNumericCheck())
				{
					try
					{
						prod = new Product();
						prod.Name = txtProductName.Text;
						prod.Price_Retail = txtRetailPrice.Text != ""?txtRetailPrice.Text:"NA";
						prod.Price_SemiRetail = txtSemiRetailPrice.Text != ""?txtSemiRetailPrice.Text:"NA";
						prod.Price_Wholesale = txtWholesalePrice.Text != ""?txtWholesalePrice.Text:"NA";
						prod.Price_SemiWholesale = txtSemiWholesalePrice.Text != ""?txtSemiWholesalePrice.Text:"NA";
						
						bool ret = prod.Add();
						if(ret == true)
						{
							MessageBox.Show("Data added successfully!","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
							btnSave.Enabled = false;
							btnCancel.Enabled = false;
							btnNew.Enabled = true;
							btnUpdate.Enabled = true;
							btnDelete.Enabled = true;
							RefreshDataGrid();
							ClearControls();
						}
					}
					catch(Exception expSave)
					{
						MessageBox.Show("Sorry... Cannot Process Your Request "+expSave.ToString());
					}
				}
				else
					MessageBox.Show("Please Enter Valid Data !!!");
			}
			else
			{
				MessageBox.Show("All Fields Are Mandatory !!!");
			}
		}
		
		void BtnCancelClick(object sender, EventArgs e)
		{
			ClearControls();
			disableAllControls();
			btnNew.Enabled = true;
			btnUpdate.Enabled = true;
			btnDelete.Enabled = true;
			btnSave.Enabled = false;
			btnCancel.Enabled = false;
			RefreshDataGrid();
		}
		
		void PopulateFields()
		{
			try
			{
				if(datagridProducts.CurrentRowIndex > -1 && dtFiltered == null)
				{
					dr = ds.Tables[0].DefaultView[datagridProducts.CurrentRowIndex].Row;
					txtProductName.Text = dr["Product_Name"].ToString();
					txtRetailPrice.Text = dr["Price_Retail"].ToString();
					txtSemiRetailPrice.Text = dr["Price_SemiRetail"].ToString();
					txtWholesalePrice.Text = dr["Price_Wholesale"].ToString();
					txtSemiWholesalePrice.Text = dr["Price_SemiWholesale"].ToString();
				}
				else
				{
					dr = dtFiltered.DefaultView[datagridProducts.CurrentRowIndex].Row;
					txtProductName.Text = dr["Product_Name"].ToString();
					txtRetailPrice.Text = dr["Price_Retail"].ToString();
					txtSemiRetailPrice.Text = dr["Price_SemiRetail"].ToString();
					txtWholesalePrice.Text = dr["Price_Wholesale"].ToString();
					txtSemiWholesalePrice.Text = dr["Price_SemiWholesale"].ToString();
				}
			}
			catch(IndexOutOfRangeException)
			{
				
			}
		}
		
		void RefreshDataGrid()
		{
			prod = new Product();
			ds = prod.View();
			datagridProducts.SetDataBinding(ds, "Products");
		}
		
		void ClearControls()
		{
			txtProductName.Text = String.Empty;
			txtRetailPrice.Text = String.Empty;
			txtSemiRetailPrice.Text = String.Empty;
			txtWholesalePrice.Text = String.Empty;
			txtSemiWholesalePrice.Text = String.Empty;
		}

		void enableAllControls()
		{
			txtProductName.Enabled = true;
			txtRetailPrice.Enabled = true;
			txtSemiRetailPrice.Enabled = true;
			txtSemiWholesalePrice.Enabled = true;
			txtWholesalePrice.Enabled = true;
		}

		void disableAllControls()
		{
			txtProductName.Enabled = false;
			txtRetailPrice.Enabled = false;
			txtSemiRetailPrice.Enabled = false;
			txtSemiWholesalePrice.Enabled = false;
			txtWholesalePrice.Enabled = false;
		}

		
		void DatagridProductsClick(object sender, EventArgs e)
		{
			enableAllControls();
			PopulateFields();
		}
		
		void DatagridProductsCurrentCellChanged(object sender, EventArgs e)
		{
			enableAllControls();
			PopulateFields();
		}
		
		void ProductsLoad(object sender, EventArgs e)
		{
			disableAllControls();
			RefreshDataGrid();
		}
		
		public bool validate()
		{
			bool validate = true;
			
			if(txtRetailPrice.Text == "" || txtProductName.Text == "" || txtWholesalePrice.Text == "" || txtSemiWholesalePrice.Text == "" || txtSemiRetailPrice.Text == "")
				validate = false;
			else
				validate = true;
			
			return validate;
		}
		public bool isNumericCheck()
		{
			bool validate = true;
			
			try
			{
				int intValue = int.Parse(txtRetailPrice.Text);
				int intValue3 = int.Parse(txtWholesalePrice.Text);
				int intValue2 = int.Parse(txtSemiRetailPrice.Text);
				int intValue4 = int.Parse(txtSemiWholesalePrice.Text);
			}
			catch(Exception)
			{
				validate = false;
			}
			return validate;
		}
		
		
		void GroupSearchClick(object sender, EventArgs e)
		{
			ProductCategorySearch prodCtgrySearch = new ProductCategorySearch();
			prodCtgrySearch.ShowDialog();
		}
		void Button1Click(object sender, EventArgs e)
		{
			Util searchUtil = new Util();
			
			string filter = string.Empty;
			string columnName = string.Empty;
			
			columnName = "Product_Name";
			
			filter = txtSearch.Text;
			//MessageBox.Show("Filter B4 : "+filter);
//			filter = "Customer_Name = '"+ds.Tables[0].DefaultView[0].Row["Customer_Name"].ToString()+"'";
			//MessageBox.Show("Filter After : "+filter);
			dtFiltered = searchUtil.Search(ds.Tables[0], filter, columnName);
			datagridProducts.DataSource = dtFiltered;
		}
	}
}
